package com.jz.banner.screen.permission

import android.app.Activity
import com.yanzhenjie.permission.Action
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.Permission


/**
 * 创建者     Noel
 * 创建时间   2018/1/31 15:11
 * 描述	      权限管理
 */
object PermissionUtil {


    fun requestLocationPermission(activity: Activity, onGranted: Action/*, onDenied: Action*/) {
        AndPermission.with(activity)
            .permission(Permission.Group.LOCATION)
            .rationale(DefaultRationale())
            .onGranted(onGranted)
            .onDenied(Action { permissions ->
                if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                    PermissionSetting(activity).showSetting(permissions)
                }
            }/*onDenied*/)
            .start()
    }

    fun requestSomePermission(activity: Activity, groups:Array<String>, onGranted: Action) {
        AndPermission.with(activity)
            .permission(groups)
            .rationale(DefaultRationale())
            .onGranted(onGranted)
            .onDenied(Action { permissions ->
                if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                    PermissionSetting(activity).showSetting(permissions)
                }
            }/*onDenied*/)
            .start()
    }

    fun requestStoragePermission(activity: Activity, onGranted: Action/*, onDenied: Action*/) {
        AndPermission.with(activity)
            .permission(Permission.Group.STORAGE)
            .rationale(DefaultRationale())
            .onGranted(onGranted)
            .onDenied(Action { permissions ->
                if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                    PermissionSetting(activity).showSetting(permissions)
                }
            }/*onDenied*/)
            .start()
    }
    fun requestCameraPermission(activity: Activity, onGranted: Action) {
        AndPermission.with(activity)
            .permission(Permission.Group.CAMERA)
            .rationale(DefaultRationale())
            .onGranted(onGranted)
            .onDenied(Action { permissions ->
                if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                    PermissionSetting(activity).showSetting(permissions)
                }
            }/*onDenied*/)
            .start()
    }

    fun requestContactsPermission(activity: Activity, onGranted: Action/*, onDenied: Action*/) {
        AndPermission.with(activity)
            .permission(Permission.Group.CONTACTS)
            .rationale(DefaultRationale())
            .onGranted(onGranted)
            .onDenied(Action { permissions ->
                if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                    PermissionSetting(activity).showSetting(permissions)
                }
            }/*onDenied*/)
            .start()
    }

    fun requestPhonePermission(activity: Activity, onGranted: Action/*, onDenied: Action*/) {
        AndPermission.with(activity)
            .permission(Permission.CALL_PHONE)
            .rationale(DefaultRationale())
            .onGranted(onGranted)
            .onDenied(Action { permissions ->
                if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
                    PermissionSetting(activity).showSetting(permissions)
                }
            }/*onDenied*/)
            .start()
    }
}